/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.Kasse;

import databaseHandling.KategoriHandler;
import java.awt.Dimension;
import java.awt.Point;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Kategori;

/**
 *
 * @author Jeppe
 */
public class KategoriPanel extends javax.swing.JPanel {

    private final KassePanel kassePanel;

    private static Point UPPER_LEFT_MARGIN = new Point(10, 80);

    /**
     * Creates new form KategoriPanel
     */
    public KategoriPanel(KassePanel kassePanel) {
        initComponents();
        this.kassePanel = kassePanel;
        
    }

    public void addCategories() {
        try {
            removeAll();
            Dimension dimension = new Dimension(130, 130);
            ArrayList<Kategori> kategoriList = KategoriHandler.hentAlleKategorier();
            
            int locationX = UPPER_LEFT_MARGIN.x;
            int locationY = -dimension.height+70;
            
            for (int i = 0; i < kategoriList.size(); i++) {
                KategoriIconPanel kategoriIconPanel = new KategoriIconPanel(this, kategoriList.get(i));
                add(kategoriIconPanel);
                
                if (i % 2 == 0) {
                    locationX = UPPER_LEFT_MARGIN.x;
//                System.out.println("JEP");
                    locationY = locationY + dimension.height+10;
                } else {
                    locationX = UPPER_LEFT_MARGIN.x + 10 + dimension.width;
                }
                kategoriIconPanel.setLocation(locationX, locationY);
                kategoriIconPanel.setSize(dimension);
//            System.out.println("kategoriNr: " + i + " | (" + locationX + "," + locationY + ")");
                
                revalidate();
                repaint();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Der kunne ikke hentes kategorier fra databasen"+ex.getLocalizedMessage(), "Database fejl", 0);
        }
    }
    
    public void visVareListePanel(Kategori kategori){
        kassePanel.showCategory(kategori);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("KategoriPanel"); // NOI18N
        setOpaque(false);
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
